package cc.chenwenxi.kod;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import cc.chenwenxi.kod.entity.KodUser;
import cc.chenwenxi.kod.entity.Result;
import cc.chenwenxi.kod.entity.ResultFiles;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.crypto.SecureUtil;
import jodd.http.HttpBrowser;
import jodd.http.HttpRequest;
import kits.ftp.FtpUpdate;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@NoArgsConstructor
@RequiredArgsConstructor
public class KodClient {
	private final HttpBrowser browser = new HttpBrowser();
	private static String TOKEN = null;

	private @NonNull String urlBase;
	private @NonNull KodUser user;

	public boolean updateFtp(String path,File file) {
		FtpUpdate ftp = new FtpUpdate();
		return ftp.uploadFile(path, file);
	}

	public ResultFiles pathList(String path) {
		Map<String, String> params = new HashMap<>();
		params.put("accessToken", this.getToken());
		params.put("path", path);
		try {
			browser.sendRequest(HttpRequest.get(urlBase + "/?explorer/pathList").query(params));
			log.info(browser.getHttpRequest().hostUrl() + browser.getHttpRequest().queryString());
			String json = browser.getPage();
			log.info("get请求:" + json);
			val result = JSONObject.parseObject(json, ResultFiles.class);
			return result;
		} catch (Exception e) {
			log.error("请求异常", e);
		}
		return new ResultFiles();
	}

	public Result mkfile(String filePath) {
		Map<String, Object> params = new HashMap<>();
		params.put("accessToken", this.getToken());
		params.put("path", filePath);
		return this.postResult("/?explorer/mkfile", params);
	}

	public Result mkdir(String folderPath) {
		Map<String, String> params = new HashMap<>();
		params.put("accessToken", this.getToken());
		params.put("path", folderPath);
		return this.getResult("/?explorer/mkdir", params);
	}

	private String getToken() {
		if (TOKEN != null) {
			return TOKEN;
		}
		log.info("TOKEN is null!! ");
		val login_token = CollUtil.join(Arrays.asList(Base64.encode(user.getUserName()),
				SecureUtil.md5(user.getUserName() + user.getApiLoginTonken())), "|");
		val params = new HashMap<String, String>();
		params.put("login_token", login_token);
		params.put("link", urlBase + "/?user/accessToken");
		val result = this.getResult("/?user/loginSubmit", params);
		log.info("token信息:" + result);
		if (result.isCode()) {
			TOKEN = result.getData().toString();
			return TOKEN;
		}
		return null;
	}

	private Result getResult(String url, Map<String, String> params) {
		try {
			browser.sendRequest(HttpRequest.get(urlBase + url).query(params));
			log.info(browser.getHttpRequest().hostUrl() + browser.getHttpRequest().queryString());
			val json = browser.getPage();
			log.info("get请求:" + json);
			val result = JSONObject.parseObject(json, Result.class);
			return result;
		} catch (Exception e) {
			log.error("请求异常", e);
		}
		return new Result();
	}

	private Result postResult(String url, Map<String, Object> params) {
		try {
			browser.sendRequest(HttpRequest.post(urlBase + url).form(params));
			log.info(browser.getHttpRequest().hostUrl() + browser.getHttpRequest().queryString());
			val json = browser.getPage();
			log.info("post请求:" + json);
			val result = JSONObject.parseObject(json, Result.class);
			return result;
		} catch (Exception e) {
			log.error("请求异常", e);
		}
		return new Result();
	}

}
